package com.myk.game.connect6tool.views;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.AttributeSet;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.TextView;

import androidx.annotation.Nullable;

/**
 * 闪烁效果视图
 */
@SuppressLint("AppCompatCustomView")
public class FlickerTextView extends TextView {

    private static final int DELAY_TIME = 1350;
    private final AlphaAnimation alphaAnimation = new AlphaAnimation(1.2f, 0.2f);

    public FlickerTextView(Context context) {
        this(context, null, 0);
    }

    public FlickerTextView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public FlickerTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        //alphaAnimation.setInterpolator(new AccelerateInterpolator());
        alphaAnimation.setDuration(DELAY_TIME);
        alphaAnimation.setRepeatCount(Animation.INFINITE);
        alphaAnimation.setRepeatMode(Animation.REVERSE);
    }

    @Override
    public void setVisibility(int visibility) {
        if (visibility == VISIBLE) {
            startAnimation(alphaAnimation);
        }
        else {
            clearAnimation();
        }
        super.setVisibility(visibility);
    }

    /**
     * 设置动画开关
     */
    public void setAnimationState(boolean isPlay) {
        clearAnimation();
        if (isPlay) {
            startAnimation(alphaAnimation);
        }
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        startAnimation(alphaAnimation);
    }

    @Override
    protected void onDetachedFromWindow() {
        clearAnimation();
        super.onDetachedFromWindow();
    }
}

